//
//  YZMSeparatorCell.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/29.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMSeparatorCell.h"
#import "YZMAppStyle.h"
#import <Masonry/Masonry.h>

typedef NS_ENUM(NSInteger, YZMSeparatorType) {
    
    YZMSeparatorTypeForTopBottom,
    YZMSeparatorTypeForMiddle,
    YZMSeparatorTypeForNone,
};

@implementation YZMSeparatorCell{
    
    UIView *_topLineView;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.leftMargin = 15.0;
        self.rightMargin = 15.0;
        
        _topLineView = [UIView new];
        _topLineView.backgroundColor = [YZMAppStyle colorForSplitLine];
        _topLineView.hidden = YES;
        [self.contentView addSubview:_topLineView];
        [_topLineView mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.top.left.right.equalTo(self);
            maker.height.mas_equalTo(0.5);
        }];
        
        _bottomLineView = [UIView new];
        _bottomLineView.backgroundColor = [YZMAppStyle colorForSplitLine];
        _bottomLineView.hidden = YES;
        [self.contentView addSubview:_bottomLineView];
        [_bottomLineView mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.bottom.left.right.equalTo(self);
            maker.height.mas_equalTo(0.5);
        }];
        
        UIView *selectedView = [[UIView alloc] init];
        selectedView.backgroundColor = [YZMAppStyle colorForSplitLine];
        self.selectedBackgroundView = selectedView;
        
        self.backgroundColor = [YZMAppStyle colorForMain];
    }
    
    return self;
}

- (void)setIndexCount:(YZMIndexCount)indexCount{
    
    if (indexCount.index >= indexCount.count) {
        
        [self layoutLine:_topLineView withLineStyle:YZMSeparatorTypeForNone];
        [self layoutLine:_bottomLineView withLineStyle:YZMSeparatorTypeForTopBottom];
        return;
    }
    
    if (indexCount.index == 0) {
        
        [self layoutLine:_topLineView withLineStyle:YZMSeparatorTypeForTopBottom];
        
        if (indexCount.count == 1) {
            
            [self layoutLine:_bottomLineView withLineStyle:YZMSeparatorTypeForTopBottom];
        }else{
            
            [self layoutLine:_bottomLineView withLineStyle:YZMSeparatorTypeForMiddle];
        }
        
        return;
    }
    
    [self layoutLine:_topLineView withLineStyle:YZMSeparatorTypeForNone];
    
    if (indexCount.index == indexCount.count -1) {
        
        [self layoutLine:_bottomLineView withLineStyle:YZMSeparatorTypeForTopBottom];
        return;
    }
    
    [self layoutLine:_bottomLineView withLineStyle:YZMSeparatorTypeForMiddle];
}

- (void)layoutLine:(UIView *)line withLineStyle:(YZMSeparatorType)lineStyle{
    
    switch (lineStyle) {
        case YZMSeparatorTypeForNone: {
            line.hidden = YES;
            break;
        }
        case YZMSeparatorTypeForMiddle: {
            line.hidden = NO;
            [line mas_updateConstraints:^(MASConstraintMaker *maker){
                
                maker.left.equalTo(self).offset(self.leftMargin);
                maker.right.equalTo(self).offset(-self.rightMargin);
            }];
            break;
        }
        case YZMSeparatorTypeForTopBottom: {
//            line.hidden = NO;
//            [line mas_updateConstraints:^(MASConstraintMaker *maker){
//                
//                maker.left.right.equalTo(self);
//            }];
            break;
        }
    }
}

@end
